package music_store.ms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import music_store.ms.common.Const;
import music_store.ms.common.ResponseCode;
import music_store.ms.common.ServerResponse;
import music_store.ms.dao.AlbumMapper;
import music_store.ms.dao.LikesMusicMapper;
import music_store.ms.dao.MusicMapper;
import music_store.ms.dao.UserMapper;
import music_store.ms.pojo.Album;
import music_store.ms.pojo.LikesMusic;
import music_store.ms.pojo.Music;
import music_store.ms.pojo.User;
import music_store.ms.service.IMusicService;
import music_store.ms.util.DateTimeUtil;
import music_store.ms.vo.MusicDetailVo;
import music_store.ms.vo.MusicListVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("iMusicService")
public class MusicServiceImpl implements IMusicService {


    @Autowired
    AlbumMapper albumMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    MusicMapper musicMapper;

    @Autowired
    LikesMusicMapper likesMusicMapper;

    @Override
    public ServerResponse<MusicDetailVo> getMusicDetailById(Integer musicId) {
        if (musicId == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        Music music = musicMapper.selectByPrimaryKey(musicId);
        if (music == null){
            return ServerResponse.createByErrorMessage("找不到该单曲。");
        }
        if (music.getStatus() != Const.ProductStatusEnum.ON_SALE.getCode()){
            return ServerResponse.createByErrorMessage("该单曲不在出售状态。");
        }
        MusicDetailVo musicDetailVo = this.assembleMusicDetailVo(music);
        return ServerResponse.createBySuccess(musicDetailVo);
    }

    public ServerResponse<PageInfo> searchMusicByMusicName(String musicName,int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        if (StringUtils.isNotBlank(musicName)){
            musicName = new StringBuilder().append("%").append(musicName).append("%").toString();
        }
        List<Music> musicList = musicMapper.selectByMusicName(musicName);
        //System.out.println(musicList.size());
        List<MusicListVo> musicListVoList = Lists.newArrayList();
        for (Music musicItem : musicList){
            MusicListVo musicListVo = this.assembleMusicListVo(musicItem);
            musicListVoList.add(musicListVo);
        }
        PageInfo pageInfo = new PageInfo(musicList);
        pageInfo.setList(musicListVoList);
        return ServerResponse.createBySuccess(pageInfo);
    }

    public ServerResponse<PageInfo> searchMusicByArtist(String artist,int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        if (StringUtils.isNotBlank(artist)){
            artist = new StringBuilder().append("%").append(artist).append("%").toString();
        }
        List<Music> musicList = musicMapper.selectByArtist(artist);
        List<MusicListVo> musicListVoList = Lists.newArrayList();
        for (Music musicItem : musicList){
            MusicListVo musicListVo = this.assembleMusicListVo(musicItem);
            musicListVoList.add(musicListVo);
        }
        PageInfo pageInfo = new PageInfo(musicList);
        pageInfo.setList(musicListVoList);
        return ServerResponse.createBySuccess(pageInfo);
    }

    public ServerResponse<PageInfo> searchMusicByAlbumName(String albumName,int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        if (StringUtils.isNotBlank(albumName)){
            albumName = new StringBuilder().append("%").append(albumName).append("%").toString();
        }
        List<Music> musicList = musicMapper.selectByAlbumName(albumName);
        List<MusicListVo> musicListVoList = Lists.newArrayList();
        for (Music musicItem : musicList){
            MusicListVo musicListVo = this.assembleMusicListVo(musicItem);
            musicListVoList.add(musicListVo);
        }
        PageInfo pageInfo = new PageInfo(musicList);
        pageInfo.setList(musicListVoList);
        return ServerResponse.createBySuccess(pageInfo);
    }

    public ServerResponse searchMusicByMusicId(Integer musicId){
        Music music = musicMapper.selectByPrimaryKey(musicId);
        if (music == null){
            return ServerResponse.createByErrorMessage("找不到该歌曲。");
        }
        return ServerResponse.createBySuccess(music);
    }

    public ServerResponse addLikesMusic (Integer musicId,Integer userId){
        if (musicId == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        Music music = musicMapper.selectByPrimaryKey(musicId);
        User user = userMapper.selectByPrimaryKey(userId);
        LikesMusic likesMusic = this.assembleLikesMusic(user,music);

        int rowCount = likesMusicMapper.insert(likesMusic);
        if (rowCount>0){
            return ServerResponse.createBySuccess("歌曲添加至‘我喜欢’成功。");
        }
        return ServerResponse.createByErrorMessage("歌曲添加至‘我喜欢’失败。");
    }

    //backend

    public ServerResponse addOrUpdateMusic(Music music){
        if (music != null){
            if (music.getId() != null){
                int rowCount = musicMapper.updateByPrimaryKeySelective(music);
                if (rowCount > 0){
                    return ServerResponse.createBySuccess("更新歌曲信息成功。");
                }
                return ServerResponse.createByErrorMessage("更新歌曲信息失败。");
            }
            else{
                int rowCount = musicMapper.insert(music);
                if (rowCount > 0){
                    return ServerResponse.createBySuccess("添加歌曲成功。");
                }
                return ServerResponse.createByErrorMessage("添加歌曲失败。");
            }
        }
        return ServerResponse.createByErrorMessage("更新或添加歌曲的参数错误。");
    }

    public ServerResponse setSaleStatus(Integer musicId , Integer status){
        if (musicId == null || status == null){
            return ServerResponse.createByErrorMessage("参数有误。歌曲id或设定的status为空。");
        }

        Music music = new Music();
        music.setId(musicId);
        music.setStatus(status);
        int rowCont = musicMapper.updateByPrimaryKeySelective(music);
        if (rowCont > 0){
            return ServerResponse.createBySuccess("歌曲销售状态更改成功。");
        }
        return ServerResponse.createByErrorMessage("歌曲销售状态更改失败。");
    }

    public ServerResponse<PageInfo> getMusicList(int pageNum, int pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<Music> musicList = musicMapper.selectMusicList();
        List<MusicListVo> musicListVoList = Lists.newArrayList();
        for (Music musicItem : musicList){
            MusicListVo musicListVo = assembleMusicListVo(musicItem);
            musicListVoList.add(musicListVo);
        }
        PageInfo pageResult = new PageInfo(musicList);
        pageResult.setList(musicListVoList);
        return ServerResponse.createBySuccess(pageResult);
    }




















    private MusicDetailVo assembleMusicDetailVo(Music music){
        MusicDetailVo musicDetailVo = new MusicDetailVo();

        musicDetailVo.setId(music.getId());
        musicDetailVo.setName(music.getName());
        musicDetailVo.setArtist(music.getArtist());
        musicDetailVo.setAlbumId(music.getAlbumId());
        musicDetailVo.setAlbumName(music.getAlbumName());
        musicDetailVo.setPrice(music.getPrice());
        musicDetailVo.setStatus(music.getStatus());
        musicDetailVo.setCreateTime(DateTimeUtil.dateToStr(music.getCreateTime()));
        musicDetailVo.setUpdateTime(DateTimeUtil.dateToStr(music.getUpdateTime()));

        return musicDetailVo;
    }

    private MusicListVo assembleMusicListVo(Music music){
        MusicListVo musicListVo = new MusicListVo();

        musicListVo.setId(music.getId());
        musicListVo.setName(music.getName());
        musicListVo.setArtist(music.getArtist());
        musicListVo.setAlbumName(music.getAlbumName());
        musicListVo.setAlbumId(music.getAlbumId());
        musicListVo.setPrice(music.getPrice());
        musicListVo.setStatus(music.getStatus());
        musicListVo.setCreateTime(DateTimeUtil.dateToStr(music.getCreateTime()));
        musicListVo.setUpdateTime(DateTimeUtil.dateToStr(music.getUpdateTime()));

        return musicListVo;
    }

    private LikesMusic assembleLikesMusic(User user, Music music){
        LikesMusic likesMusic = new LikesMusic();

        likesMusic.setUserId(user.getId());
        likesMusic.setUserName(user.getUsername());
        likesMusic.setMusicId(music.getId());
        likesMusic.setMusicName(music.getName());
        likesMusic.setMusicArtist(music.getArtist());
        likesMusic.setAlbumId(music.getAlbumId());
        likesMusic.setAlbumName(music.getAlbumName());

        return likesMusic;
    }
}
